<template>
	<view class="content">
		<view class="avatarWrapper">
			<view class="avatar">
				<image class="img" src="../../static/icon/start.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="form">
			<view class="inputWrapper">
				<input type="text" v-model="email" placeholder="请输入邮箱" />
			</view>
			<view class="inputWrapper">
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="loginBtn" @tap="toLogin">
				<text>登录</text>
			</view>
			<view class="register" @tap="toRegister">
				<text>没有账号，立即注册</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '5566@qq.com',
				password: '555666'
			}
		},
		onLoad() {},
		methods: {
			//登录
			toLogin() {
				//条件判断，邮箱不能为空
				if (this.email.length == 0) {
					uni.showToast({
						title: '请输入邮箱！',
						icon: 'none'
					})
				} else if (this.password.length == 0) {
					//密码不能为空
					uni.showToast({
						title: '请输入密码！',
						icon: 'none'
					})
				} else {
					//登录验证
					this.$post(this.$api.USER_LOGIN, {
						id: "0",
						email: this.email,
						mpassword: this.password,
					}).then(res => {
						//保存用户id,为后面用户上传图片使用
						uni.setStorage({
							key: 'user_id',
							data: res.content.id,
							success() {
								//登录验证成功
								if (res.code == 200) {
									uni.switchTab({
										url: '../home/home',
										success() {
											uni.showToast({
												title: '登录成功',
												icon: 'success'
											})
										}
									})
								} else {
									//登录验证失败
									uni.showToast({
										title: '登录失败',
										icon: 'none'
									})
								}
							},
							//保存用户id失败,同时登录也会失败
							fail() {
								//登录验证异常
								uni.showToast({
									title: '登录异常',
									icon: 'error'
								})
							}
						})
					}).catch((e) => {
						//登录验证异常
						uni.showToast({
							title: '登录异常',
							icon: 'error'
						})
					})
				}
			},
			//注册，跳到注册页面
			toRegister() {
				uni.navigateTo({
					url: "/pages/register/register"
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background: #33a69e;
		width: 100vw;
		height: 100vh;

		.avatarWrapper {
			height: 30vh;
			width: 100vw;
			display: flex;
			justify-content: center;
			align-items: flex-end;

			.avatar {
				width: 200upx;
				height: 200upx;
				overflow: hidden;

				.img {
					width: 100%;
				}
			}
		}

		.form {
			padding: 0 100upx;
			margin-top: 50px;

			.inputWrapper {
				width: 100%;
				height: 80upx;
				background: white;
				border-radius: 20px;
				box-sizing: border-box;
				padding: 0 20px;
				margin-top: 25px;

				input {
					width: 100%;
					height: 100%;
					text-align: center;
					font-size: 15px;
				}
			}

			.loginBtn {
				width: 100%;
				height: 80upx;
				background: #77B3D7;
				border-radius: 50upx;
				margin-top: 50px;
				display: flex;
				justify-content: center;
				align-items: center;

				text {
					color: white;
				}
			}

			.register {
				text-align: center;
				color: #EAF6F9;
				font-size: 15px;
				margin-top: 20px;
			}
		}
	}
</style>
